//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/ienum/question/first_type_enum.dart';
import 'package:hktk_app/ienum/question/question_type_enum.dart';
import 'package:hktk_app/model/question/question_data_model.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/sz_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/question/stem_rich_text_widget.dart';

/// 选项组件
class StemOptionWidget extends StatelessWidget {
  /// 试题模式，ture-答题，false-背题
  final QuestionTypeEnum type;

  /// 选项相关数据
  final List<OptionModel> optionList;

  /// 选项答案
  final List<String> answer;

  /// 试题类型
  final FirstTypeEnum qType;

  /// 是否显示答案
  final bool showDa;

  /// 选中响应方法
  final ValueChanged<String> selectedChanged;

  /// 选中的选项
  final List<String> selectedList;

  const StemOptionWidget({
    super.key,
    required this.optionList,
    required this.answer,
    required this.showDa,
    required this.selectedChanged,
    required this.selectedList,
    required this.type,
    required this.qType,
  });

  @override
  Widget build(BuildContext context) {
    debugPrint("stemOptionWidget 组件 -> build 方法执行 ===> ");
    debugPrint("");
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: _items(),
    );
  }

  /// 选项
  _items() {
    List<Widget> widgets = [];
    for (int i = 0; i < optionList.length; i++) {
      OptionModel model = optionList[i];
      widgets.add(_item(model));
      // 添加间隔
      if (i != optionList.length - 1) {
        widgets.add(hiSpace(height: 50));
      }
    }
    return widgets;
  }

  /// 显示选项方法
  Widget _item(OptionModel model) {
    return GestureDetector(
      onTap: () {
        debugPrint("stemOptionWidget 组件 -> 你点击了 ===> ${model.option}，是否显示答案：$showDa");
        if (type == QuestionTypeEnum.answer) {
          if (!showDa) {
            selectedChanged(model.option);
          }
        } else if (type == QuestionTypeEnum.exam) {
          selectedChanged(model.option);
        }
      },
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _showLabel(model.option),
          hiSpace(width: 20),
          Expanded(
            child: StemRichTextWidget(
              text: model.content,
            ),
          ),
        ],
      ),
    );
  }

  /// 显示AB...标签。
  /// optionV：标签，ABCD，
  _showLabel(String optionV) {
    String showV = optionV;
    if (MyUtil.getPDT(qType)) {
      showV = "";
    }
    // 标签组件
    Widget label;
    // 是否是背题模式
    if (type != QuestionTypeEnum.memory) {
      // 做题模式，判断是否选中
      bool selected = selectedList.contains(optionV);
      // 是否选中当前选项
      if (selected) {
        // 选中，判断是否需要显示当前答案
        if (showDa) {
          // 显示答案
          label = _showSuc(answer.contains(optionV));
        } else {
          // 显示选中效果
          label = _showSel(true, showV);
        }
      } else {
        // 是否是答案
        if (showDa && answer.contains(optionV)) {
          if (type == QuestionTypeEnum.check ||
              type == QuestionTypeEnum.dailyRecord) {
            // 这里使用正确显示
            label = _showSuc(true);
          } else {
            // 将正确答案标识出来，使用选中的效果
            label = _showSel(true, showV);
          }
        } else {
          // 未选中样式
          label = _showSel(false, showV);
        }
      }
    } else {
      // 背题模式
      if (answer.contains(optionV)) {
        // 正确
        label = _showSuc(true);
      } else {
        label = _showSel(false, showV);
      }
    }
    return label;
  }

  /// 显示是否选中方法
  _showSel(bool selected, String option) {
    return _showRound(
      backgroundColor: selected
          ? getColorBlue1(szYjms: szUtilComm.szInfo.yjms)
          : getColorWhite1b12(szYjms: szUtilComm.szInfo.yjms),
      showBorder: !selected,
      childWidget: Text(
        option,
        style: TextStyle(
          fontSize: szUtilComm.szInfo.getZtdxPx(),
          fontWeight: FontWeight.w400,
          color: selected ? colorWhite1 : colorGrey2,
        ),
      ),
    );
  }

  /// 显示正确或错误标签
  _showSuc(bool success) {
    return _showRound(
      backgroundColor: success
          ? getColorBlue1(szYjms: szUtilComm.szInfo.yjms)
          : getColorRed5(szYjms: szUtilComm.szInfo.yjms),
      showBorder: false,
      childWidget: SvgPicture.asset(
        success
            ? "images/question/stem_option_success.svg"
            : "images/question/stem_option_error.svg",
        width: 30,
        height: 30,
      ),
    );
  }

  /// 显示圆
  _showRound({
    required Color backgroundColor,
    required bool showBorder,
    required Widget childWidget,
  }) {
    return Container(
      width: 30,
      height: 30,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.all(Radius.circular(28)),
        border: showBorder
            ? Border.all(
          color: colorGrey6,
          width: 1,
        )
            : null,
      ),
      child: Center(
        child: childWidget,
      ),
    );
  }
}
